﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebEntities;
using WebEntities.Models.WebInfo;

namespace com.subb.WebInfo
{
    public class WI_HonorDAL
    {
        public List<WI_Honor> GetAllHonors(int start, int limit, out int ttlCount)
        {
            ttlCount = 0;
            SugardbEntities ctx = new SugardbEntities();
            var query = ctx.WI_Honor.OrderByDescending(c => c.SortNum ?? 0).ThenByDescending(c => c.CreateTime);
            ttlCount = query.Count();
            List<WI_Honor> honorlist = query.Skip(start).Take(limit).ToList();
            return honorlist;
        }

        public List<WI_Honor> GetAllHonors(int honTypeId, int start, int limit, out int ttlCount)
        {
            ttlCount = 0;
            SugardbEntities ctx = new SugardbEntities();
            var query = ctx.WI_Honor.Where(c => c.HonorTypeID == honTypeId).OrderByDescending(c => c.SortNum ?? 0).ThenByDescending(c => c.CreateTime);
            ttlCount = query.Count();
            List<WI_Honor> honorlist = query.Skip(start).Take(limit).ToList();
            return honorlist;
        }

        public List<WI_Honor> GetAllHonors()
        {
            SugardbEntities ctx = new SugardbEntities();
            var query = ctx.WI_Honor.OrderByDescending(c => c.SortNum ?? 0).ThenByDescending(c => c.CreateTime);
            List<WI_Honor> honorlist = query.ToList();
            return honorlist;
        }

        public List<WI_Honor> GetAllHonors(int honTypeId)
        {
            SugardbEntities ctx = new SugardbEntities();
            var query = ctx.WI_Honor.Where(c => c.HonorTypeID == honTypeId).OrderByDescending(c => c.SortNum ?? 0).ThenByDescending(c => c.CreateTime);
            List<WI_Honor> honorlist = query.ToList();
            return honorlist;
        }

        public WI_Honor GetHonor(int horId)
        {
            SugardbEntities ctx = new SugardbEntities();
            WI_Honor job = ctx.WI_Honor.FirstOrDefault(c => c.HonorID == horId);
            return job;
        }

        public int CreateHonor(Honor vhor)
        {
            int horId = 0;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                WI_Honor newhor = new WI_Honor
                {
                    HonorName = vhor.HonorName,
                    HonorTypeID = vhor.HonorTypeId,
                    AwardUnit = vhor.AwardUnit,
                    AwardTime = vhor.AwardTime,
                    TitleImg = vhor.TitleImg,
                    OriginImg = vhor.OriginImg,
                    CreateTime = DateTime.Now,
                    Creator = vhor.Creator,
                    SortNum = vhor.SortNum,
                    ImgType = vhor.ImgType
                };
                ctx.WI_Honor.AddObject(newhor);
                ctx.SaveChanges();
                horId = newhor.HonorID;
            }
            return horId;
        }

        public bool UpdateHonor(int horId, Honor vhor)
        {
            bool result = false;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                WI_Honor wihor = ctx.WI_Honor.SingleOrDefault(c => c.HonorID == horId);
                if (wihor != null)
                {
                    wihor.HonorName = vhor.HonorName;
                    wihor.HonorTypeID = vhor.HonorTypeId;
                    wihor.AwardUnit = vhor.AwardUnit;
                    wihor.AwardTime = vhor.AwardTime;
                    wihor.TitleImg = vhor.TitleImg;
                    wihor.OriginImg = vhor.OriginImg;
                    wihor.SortNum = vhor.SortNum;
                    wihor.ImgType = vhor.ImgType;
                    ctx.SaveChanges();
                    result = true;
                }
            }
            return result;
        }

        public bool RemoveHonor(int horId)
        {
            bool result = false;
            using (SugardbEntities ctx = new SugardbEntities())
            {
                WI_Honor wiaItem = ctx.WI_Honor.SingleOrDefault(c => c.HonorID == horId);
                if (wiaItem != null)
                {
                    ctx.WI_Honor.DeleteObject(wiaItem);
                    ctx.SaveChanges();
                    result = true;
                }
            }
            return result;
        }
    }
}
